//
//  BSComtentTableViewController.m
//  BS
//
//  Created by 王浩宇 on 15/8/17.
//  Copyright (c) 2015年 lanou. All rights reserved.
//
#define kURL @"http://api.budejie.com/api/api_open.php?a=dataList&c=comment&hot=1&per=50"

#import "BSComtentTableViewController.h"

#import "BSCommtentTableViewCell.h"

#import "LJRequestManagerBlock.h"

#import "BSComtentDataModel.h"

#import "BSComtentHotModel.h"

#import "Header.h"

@interface BSComtentTableViewController ()

@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, strong)NSMutableArray *hotArray;

@property (nonatomic, strong)UIMenuController *menuController;

@property (strong, nonatomic)NSMutableDictionary *parameterDic;//参数字典

@end

@implementation BSComtentTableViewController

- (instancetype)initWithStyle:(UITableViewStyle)style{

    if (self = [super initWithStyle:style]) {
        self.hidesBottomBarWhenPushed=YES;
    }
    return self;
}


- (NSDictionary *)parameterDic{
    
    if (_parameterDic == nil) {
        _parameterDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    }
    return _parameterDic;
}


- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}


- (NSMutableArray *)hotArray
{
    if (_hotArray == nil) {
        _hotArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _hotArray;
}

- (void)fildData:(NSDictionary *)parameterDic
{
   
    [LJRequestManagerBlock manager:GET requestWith:kURL dic:parameterDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = dic[@"data"];
        NSArray *hotArray = dic[@"hot"];
        for (NSDictionary *dataDic in array) {
            BSComtentDataModel *dataModel = [[BSComtentDataModel alloc] init];
            [dataModel setValuesForKeysWithDictionary:dataDic];
            [self.dataArray addObject:dataModel];
        }
        for (NSDictionary *hotDic in hotArray) {
            BSComtentHotModel *model = [[BSComtentHotModel alloc] init];
            [model setValuesForKeysWithDictionary:hotDic];
            [self.hotArray addObject:model];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self.parameterDic setValue:self.data_id forKey:@"data_id"];
    [self.parameterDic setValue:@"1" forKey:@"page"];
    [self fildData:self.parameterDic];
    _menuController = [[UIMenuController alloc] init];
    UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(doPraise)];
    UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle:@"回复" action:@selector(doreply)];
    UIMenuItem *item3 = [[UIMenuItem alloc] initWithTitle:@"举报" action:@selector(doReport)];
    _menuController.menuItems = @[item1, item2, item3];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BSCommtentTableViewCell" bundle:nil] forCellReuseIdentifier:@"comment"];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (BOOL)canBecomeFirstResponder
{
    return YES;
}
//赞
- (void)doPraise
{
    
}
//回复
- (void)doreply
{
    
}
//举报
- (void)doReport
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if (section == 0) {
        return _hotArray.count;
    }else {
        return _dataArray.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BSCommtentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        BSComtentHotModel *hotModel = _hotArray[indexPath.row];
        cell.hotModel = hotModel;
    }else if(indexPath.section == 1){
        BSComtentDataModel *dataModel = _dataArray[indexPath.row];
        cell.dataModel = dataModel;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        BSComtentHotModel *hotModel = _hotArray[indexPath.row];
        return [BSCommtentTableViewCell cellHeigh:hotModel];
    }else{
        BSComtentDataModel *dataModel = _dataArray[indexPath.row];
        return [BSCommtentTableViewCell cellHeight:dataModel];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    
    if (section == 0) {
        header.text = @"  最热评论";
    }else
    {
        header.text = @"  最新评论";
    }
    header.font = [UIFont systemFontOfSize:14];
    header.backgroundColor = [UIColor whiteColor];
    
    return header;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BSCommtentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment" forIndexPath:indexPath];
    [_menuController setTargetRect:cell.frame inView:self.view];
    [_menuController setMenuVisible:YES animated:YES];


}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
